using System.Linq;
using HashFoo.NhibTests;
using HashFoo.NhibTests.DomainFixtures;
using HashFoo.NhibTests.DomainFixtures.Models;
using HashFoo.SqlServer.Spatial;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NHibernate.Linq;

namespace HashFoo.Nhib.SpatialTests
{
    [TestClass]
    public class CustomTypesTests
    {
        [TestMethod]
        public void can_map_hashfoogeography_type()
        {
            Helpers.NukeDatabase();

            var migrationManager = Helpers.CreateDefaultMigrationManagerWorker();
            migrationManager.Create();

            var field = new FieldBoundary
                        {
                            Boundary = Geography.CreateSquarePolygon(5, 5, 10, 10)
                        };

            field.Boundary.Srid = 4326;

            // get session and create some field boundaries!!!
            using (var sessionFactory = MockConfigurator.DefaultConfiguration.BuildSessionFactory())
            using (var session = sessionFactory.OpenSession())
            {
                session.Save(field);

                session.Flush();
                session.Clear();

                Assert.IsTrue(session.Query<FieldBoundary>().Any());

                var actualField = session.Query<FieldBoundary>().FirstOrDefault();
                Assert.IsNotNull(actualField);

                Assert.IsTrue(actualField.Boundary.EqualsSpatially(field.Boundary));

                Assert.AreEqual(4326, field.Boundary.Srid);

                session.Close();
                sessionFactory.Close();
            }

        }

        //[TestMethod]
        //[Description("Tests that enumeration classes can be mapped and saved")]
        //public void CanMapAndSaveEnumerationProperties()
        //{
        //    Helpers.NukeDatabase();

        //    var migrationManager = Helpers.CreateDefaultMigrationManagerWorker();
        //    migrationManager.Create();

        //    var field = new FieldBoundary
        //                {
        //                    Boundary = Spatial.GeometryUtil.CreateSquarePolygon(
        //                        new Coordinate(10, 0), new Coordinate(0, 10)),
        //                    FieldType = DomainFixtures.Enumerations.FieldType.Corn
        //                };

        //    // get session and create some field boundaries!!!
        //    using (var sessionFactory = DomainFixtures.MockConfigurator.DefaultConfiguration.BuildSessionFactory())
        //    using (var session = sessionFactory.OpenSession())
        //    {
        //        session.Save(field);

        //        session.Flush();
        //        session.Clear();

        //        Assert.IsTrue(session.Query<FieldBoundary>().Count() > 0);

        //        var actualField = session.Query<FieldBoundary>().FirstOrDefault();
        //        Assert.IsNotNull(actualField);

        //        Assert.IsTrue(actualField.FieldType.Value == DomainFixtures.Enumerations.FieldType.Corn.Value);

        //        session.Close();
        //        sessionFactory.Close();
        //    }
        //}

        //[TestMethod]
        //[Description("Tests that enumeration classes correctly mapped in a query")]
        //[ExpectedException(ExpectedException = typeof(NHibernate.HibernateException), UserMessage = "there is a bug in nhib linq that doesn't allow linq queries using custom types")]
        //public void CanQueryAgainstEnumerationType()
        //{
        //    Helpers.NukeDatabase();

        //    var migrationManager = Helpers.CreateDefaultMigrationManagerWorker();
        //    migrationManager.Create();

        //    var field = new FieldBoundary
        //    {
        //        Boundary = Spatial.GeometryUtil.CreateSquarePolygon(new Coordinate(10, 0), new Coordinate(0, 10)),
        //        FieldType = FieldType.Corn
        //    };

        //    // get session and create some field boundaries!!!
        //    using (var sessionFactory = DomainFixtures.MockConfigurator.DefaultConfiguration.BuildSessionFactory())
        //    using (var session = sessionFactory.OpenSession())
        //    {
        //        session.Save(field);

        //        session.Flush();
        //        session.Clear();

        //        Assert.IsTrue(session.Query<FieldBoundary>().Count() > 0);

        //        var actualField = session.Query<FieldBoundary>().Where(f => f.FieldType == FieldType.Corn).FirstOrDefault();
        //        Assert.IsNotNull(actualField);

        //        Assert.IsTrue(actualField.FieldType.Value == FieldType.Corn.Value);

        //        session.Close();
        //        sessionFactory.Close();
        //    }
        //}

        //[TestMethod]
        //[Description("Tests that enumeration classes correctly mapped in a query")]
        //public void CanMapAndQueryEnum()
        //{
        //    Helpers.NukeDatabase();

        //    var migrationManager = Helpers.CreateDefaultMigrationManagerWorker();
        //    migrationManager.Create();

        //    var field = new FieldBoundary
        //    {
        //        Boundary = Spatial.GeometryUtil.CreateSquarePolygon(
        //            new Coordinate(10, 0), new Coordinate(0, 10)),
        //        FieldType = FieldType.Corn,
        //        GrowerType = GrowerType.Hillbilly
        //    };

        //    // get session and create some field boundaries!!!
        //    using (var sessionFactory = DomainFixtures.MockConfigurator.DefaultConfiguration.BuildSessionFactory())
        //    using (var session = sessionFactory.OpenSession())
        //    {
        //        session.Save(field);

        //        session.Flush();
        //        session.Clear();

        //        Assert.IsTrue(session.Query<FieldBoundary>().Count() > 0);

        //        var actualField = session.Query<FieldBoundary>().Where(f => f.GrowerType == GrowerType.Hillbilly).FirstOrDefault();
        //        Assert.IsNotNull(actualField);

        //        Assert.IsTrue(actualField.GrowerType == GrowerType.Hillbilly);

        //        session.Close();
        //        sessionFactory.Close();
        //    }
        //}

        //[TestMethod]
        //[Description("Tests that symbol enumeration classes can be mapped and saved")]
        //public void CanMapAndSaveSymbolEnumeration()
        //{
        //    Helpers.NukeDatabase();

        //    var migrationManager = Helpers.CreateDefaultMigrationManagerWorker();
        //    migrationManager.Create();

        //    var field = new FieldBoundary
        //    {
        //        Boundary = Spatial.GeometryUtil.CreateSquarePolygon(
        //            new Coordinate(10, 0), new Coordinate(0, 10)),
        //        FieldType = FieldType.Corn,
        //        FarmerType = FarmerType.Big
        //    };

        //    // get session and create some field boundaries!!!
        //    using (var sessionFactory = DomainFixtures.MockConfigurator.DefaultConfiguration.BuildSessionFactory())
        //    using (var session = sessionFactory.OpenSession())
        //    {
        //        session.Save(field);

        //        session.Flush();
        //        session.Clear();

        //        Assert.IsTrue(session.Query<FieldBoundary>().Count() > 0);

        //        var actualField = session.Query<FieldBoundary>().FirstOrDefault();
        //        Assert.IsNotNull(actualField);

        //        Assert.IsTrue(actualField.FarmerType == FarmerType.Big);

        //        session.Close();
        //        sessionFactory.Close();
        //    }
        //}


    }


}